﻿using Lucene.Net.Analysis.Standard;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using AliveCMS.Models;
using System.Threading.Tasks;
namespace AliveCMS
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static Directory SiteData = FSDirectory.Open(HttpContext.Current.Server.MapPath(WebConfigurationManager.AppSettings["SiteDataLocation"]));

        public static Dictionary<Guid,SiteViewModel> Sites = new Dictionary<Guid,SiteViewModel>();
        public static List<PageViewModel> Pages = new List<PageViewModel>();
        public static List<ContentAreaViewModel> ContentAreas = new List<ContentAreaViewModel>();


        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            MvcApplication.LoadData();
        }

        public override void Dispose()
        {
            MvcApplication.SiteData.Dispose();
            base.Dispose();
        }

        private static void LoadData()
        {
            using (var indexSearcher = new IndexSearcher(MvcApplication.SiteData))
            {
                Parallel.Invoke(
                delegate()
                {
                    foreach (var site in indexSearcher.Search(new TermQuery(new Term("ObjectName", "SiteViewModel")), int.MaxValue).ScoreDocs)
                    {
                        MvcApplication.Sites.Add(Guid.Parse(indexSearcher.Doc(site.Doc).Get("SiteId")), new SiteViewModel()
                        {
                            SiteId = Guid.Parse(indexSearcher.Doc(site.Doc).Get("SiteId")),
                            Name = indexSearcher.Doc(site.Doc).Get("Name"),
                            Domains = indexSearcher.Doc(site.Doc).Get("Domains").Split(',').ToList()
                        });
                    }
                },
                delegate()
                {
                    foreach (var page in indexSearcher.Search(new TermQuery(new Term("ObjectName", "PageViewModel")), int.MaxValue).ScoreDocs)
                    {
                        MvcApplication.Pages.Add(new PageViewModel()
                        {
                            PageId = Guid.Parse(indexSearcher.Doc(page.Doc).Get("PageId")),
                            Name = indexSearcher.Doc(page.Doc).Get("Name"),
                            ParentPageId = Guid.Parse(indexSearcher.Doc(page.Doc).Get("ParentPageId"))
                        });
                    }
                });

            }
        }

    }
}
